;;
;; @File    le-emacs-auto-complete.el
;; @Author  Gan XiangLe
;; @Date    2011/10/28
;; @update  2011/12/23
;; @in      EMACS-DIR, a path string of emacs directory that contains le_plugins/
;;          and le_emacs_conf/
;; @Usage  (1)Install auto-complete plugin,
;;         (2)Add the auto-complete plugin path to Emacs load path.
;;         (3)Add the dict/ dir to ac-dictionary-directories
;;         (4)Load this config file.
;;         
;;    [INSTALL And Config of auto-complete plugin]
;;      * See Online Manual: http://cx4a.org/software/auto-complete/manual.html
;;         (1)Download auto-complete-1.3.1.zip, and unpack it to
;;            Emacs user plugin dir(for your favarot where it is)
;;         (2)First, do byte-compile all .el files in the package directory
;;            $ cd */auto-complete-1.3.1/
;;            $ make byte-compile 
;;            (Invoking emacs to do byte compile, See Makefile for detail)
;;         (3)Dictionary files are located in called dict/ directory.
;;            (add-to-list 'ac-dictionary-directories "*/auto-complete*/dict")
;;    [Usage]
;;         It is necessary to type TAB a few times for completion by TAB.
;;         Completion by RET instead complete a selected candidate immediately.
;;      * Keys
;;         (1) TAB: whill complete 1 candidate, part of common candicdates, or
;;             _cycle_ all candicdates     
;;         (2) RET: complete 1 candidate immediately, exec an action if
;;             a seleted candidate has the action
;;      * Keys When ac start(inserting chars triggers)
;;         +------------+----------------+---------------------------+
;;         |        Key | Command        | Description               |
;;         +------------+----------------+---------------------------+
;;         |   TAB, C-i | ac-expand      | Completion by TAB         |
;;         |   RET, C-m | ac-complete    | Completion by RET         |
;;         |  down, M-n | ac-next        | Select next candidate     |
;;         |    up, M-p | ac-previous    | Select previous candidate |
;;         |    C-?, f1 | ac-help        | Show buffer help          |
;;         +------------+----------------+---------------------------+
;;         To stop completion, use C-g simply.
;;         To filter completion candidates, use C-s.

(defvar le-auto-complete-path
  (concat EMACS_DIR "le_plugins/auto-complete-1.3.1"))
(defvar le-auto-complete-dict-path
  (concat EMACS_DIR "le_plugins/auto-complete-1.3.1/dict"))

(add-to-list 'load-path le-auto-complete-path)

(require 'auto-complete-config)

(add-to-list 'ac-dictionary-directories le-auto-complete-dict-path)

(ac-config-default)

;; If don't want to start auto-complete-mode automatically.
;; you can use auto-complete func, an bound to a KEY to
;; manually start.
;; you can also set *Trigger Key* for auto-complete instead of 'define-key'
;; Trigger key will be enabled after inserting characters.Otherwise it is
;; dealt as an usual command.(if TAB, it's indentation when NOT inserting
;; chars).
;; NOTE: auto-complete command is littele different from
;; original automatic completion(has the fuzzy matching)
;(ac-set-trigger-key "TAB")
;(setq ac-auto-start nil)

;; Make complete smarter: Candidate Suggestion
;; Analyzes completion operations and put frequency used candicate to TOP
;; of completion menu.
;; It is called COMPHIST internally, and you can use it by setting
;; ac-use-comphist to t. It is enabled by default. Collection operations
;; data will be stored in user-emacs-directory or ~/.emacs.d/ with a
;; name ac-comphist.dat.
;; nil means never using it and get PERFORMANCE better maybe.
;(setq ac-use-comphist nil)

;; if you allow to start completion automatically but also want to be silent
;; as much as possible, you can do it by setting ac-auto-start to an prefix
;; length integer.
;; Lesser ac-auto-start, more high COST to produce completion candidates.
(setq ac-auto-start 2)

;; There is another approach to solve the annoying problem is that not to
;; show completion menu automatically.
;; When you select or filter candidates, completion menu will be shown.
;(setq ac-auto-show-menu nil)
;; In other way, you can delay showing completion menu by setting
;; ac-auto-show-menu to seconds in real number.
(setq ac-auto-show-menu 0.8)

;; You can stop completion by pressing C-g. Or:
;(define-key ac-completing-map "\M-/" 'ac-stop)

;; Bind another key to ac-complete if you don't like RET(default)
(define-key ac-completing-map "\b" 'ac-complete)

;; You can select candidates with C-n/C-p
;; it is better that selecting candidates is enabled only when completion
;; menu is displayed.ac-menu-map is a keymap for completion on completion menu.
;(setq ac-use-menu-map t)
;(define-key ac-menu-map "\C-n" 'ac-next)
;(define-key ac-menu-map "\C-p" 'ac-previous)

;; A tooltip help that is shown when completing, called quick help, disable it.
(setq ac-use-quick-help nil)

;; Set ac-menu-height to number of lines.
(setq ac-menu-height 10)

;; auto-complete-mode won't be enabled automatically for modes that are not
;; in ac-modes.
(add-to-list 'ac-modes 'markdown-mode)

;; Colors settings, Available colors can be seen with M-x list-colors-display.
(set-face-background  'ac-candidate-face "LightBlue")
(set-face-underline-p 'ac-candidate-face "CornflowerBlue")
(set-face-background  'ac-selection-face "RoyalBlue")

;; Whether or not to use fuzzy matching
(setq ac-use-fuzzy t)

;; Whether or not to stop Flymake on completion.
(setq ac-stop-flymake-on-completing t)

;; "Do What I Mean"
(setq ac-dwim t)

;; By default, ~/.dict will be a dictionary file.
;; User defined dictionary is composed of a list of string specified
;; ac-user-dictionary
(add-to-list 'ac-user-dictionary user-mail-address)

;;
(defun my-c-mode-hook-for-auto-complete ()
  (setq ac-sources '(ac-source-functions
                     ac-source-variables
                     ac-source-yasnippet
                     ac-source-symbols
                     ac-source-features
                     ;ac-source-abbrev
                     ac-source-filename                     
                     ac-source-words-in-same-mode-buffers
                     ac-source-dictionary)))
(add-hook 'c-mode-hook 'my-c-mode-hook-for-auto-complete())





